package com.visualexpress.predict;

import java.util.List;

import com.visualexpress.dao.RouteInfoDAO;
import com.visualexpress.domain.Location;
import com.visualexpress.domain.RouteInfo;

public class MeanTimeBetweenCities {
	public static double meanTimeBetweenCitiesInHours(Location loc1, Location loc2) {
		if(loc1.equals(loc2)) {
			return GetAverageTimeForCity.getAverageTimeForCity(loc1);
		}
		RouteInfoDAO routeInfoDAO = new RouteInfoDAO();
		
		List<RouteInfo> routeInfoList = routeInfoDAO.findByStartAndEnd(loc1, loc2);
		if(routeInfoList.isEmpty()) {
			return -1.0;
		}
		RouteInfo routeInfo = routeInfoList.get(0);
		double timeAtLoc1 = GetAverageTimeForCity.getAverageTimeForCity(loc1);
		double timeAtLoc2 = GetAverageTimeForCity.getAverageTimeForCity(loc2);
		if(timeAtLoc1 != -1.0 && timeAtLoc2 != -1.0) {
			return routeInfo.getTime() + timeAtLoc1 + timeAtLoc2;
		} else {
			return -1.0;			
		}
	}
}
